<?php

class BlogController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    
    public function indexAction() {
        $blogData = array();
        $blogModel = new Model_Blog();
        $blogData = $blogModel->fetchBlogWITHComment();
        $recentData = $blogModel->fetchBlogWITHComment("", "5", "", "blog_id", "desc");
        
        
        $this->view->recentData = $recentData;
        
        $this->view->blogData = $blogData;
    }
    
    public function blogdetailAction() {
        $blogData = array();
        $commentData = array();
        $errorMessage = array();
        $successmessage = "";
        $blogModel = new Model_Blog();
        $commentModel = new Model_Comment();
        
        if ($this->_getParam('blog_id') && $this->_getParam('blog_id') != '') {
                $blog_id = $this->_getParam('blog_id');
        }else {
                $this->_helper->redirector('index', 'blog', 'default');
        }
        
        $blogData = $blogModel->fetchBlogWITHComment("blog_id = ".$blog_id);
        $recentData = $blogModel->fetchBlogWITHComment("", "5", "", "blog_id", "desc");
        
        $next_blogid = $blogModel->NEXTblogid($blog_id);
        $prev_blogid = $blogModel->PREVIOUSblogid($blog_id);
        
        
         if ($this->getRequest()->isPost()) {
            
              $postedData = $this->_request->getPost();
             
              if($postedData['name'] == 'Name:'){
                  $errorMessage[] = "Please enter the name";
              }if($postedData['email'] == 'Email:'){
                  $errorMessage[] = "Please enter the email";
              }if (!filter_var($postedData['email'], FILTER_VALIDATE_EMAIL)) {
                $errorMessage[] = "Invalid email address"; 
              }if($postedData['mobile'] == 'Contact no:'){
                  $errorMessage[] = "Please enter the mobile number";
              }if($postedData['message'] == ''){
                  $errorMessage[] = "Please enter the message";
              }
              
              
              if (count($errorMessage) > 0) {
              } else {
                  $commentData['cmt_blog_id'] = $blog_id;
                  $commentData['cmt_name'] = $postedData['name'];
                  $commentData['cmt_email'] = $postedData['email'];
                  $commentData['cmt_phone_no'] = $postedData['mobile'];
                  $commentData['cmt_note'] = $postedData['message'];
                  $commentData['cmt_createddate'] = date('Y-m-d H:i:s');
                  $commentData['cmt_updateddate'] = date('Y-m-d H:i:s');
                  $commentData['cmt_status'] = 1;
                  $commentModel->save($commentData, 'cmt_id');
                    
                    $successmessage = 'Comment add successfully';
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('blogdetail', 'blog', 'default',array("blog_id"=>$blog_id));
              }
             
         }
        
         
        $this->view->next_blogid = $next_blogid;
        $this->view->prev_blogid = $prev_blogid;
        $this->view->recentData = $recentData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->blogData = $blogData;
    }

    

}

?>
